<?php
require 'cabesario.php';
require 'model/Contact.php';
if (isset($_SESSION["autenticacao"])) {
    if (isset($_POST)) {
        if (isset($_POST['action'])) {
            $action = $_POST['action'];
            if ($action == 'editar') {
                $list = $_POST['id_contact'];
                ?><form method="post" action="#"><input type="hidden" name="action" value="ok"><?php
                    foreach ($list as $id) {
                        foreach ($_SESSION['session_contacts'] as $a) {
                            if ($a['id'] == $id) {
                                ?><input class="input_text" type="hidden" name="id_contact[]" value="<?php echo $a['id'] ?>">
                                <input class="input_text" type="text" name="name_contact[]" required="true" placeholder="Nome" value="<?php echo $a['name'] ?>">
                                <input class="input_text" type="email" name="email_contact[]" required="true" placeholder="E-mail" value="<?php echo $a['email'] ?>">
                                <input class="input_text input_fone" type="text" name="fone_contact[]" placeholder="Fone" value="<?php echo $a['fone'] ?>"/><br/><hr/><?php
                            }
                        }
                    }
                    ?><input type="submit" value="Ok" class="style_button"> <a class="style_button" href="import.php">Voltar</a></form><?php
                } else if ($action == 'ok') {
                    $id_list = $_POST['id_contact'];
                    $name_list = $_POST['name_contact'];
                    $email_list = $_POST['email_contact'];
                    $fone_list = $_POST['fone_contact'];
                    $session_contacts = $_SESSION['session_contacts'];
                    foreach ($id_list as $key => $co) {
                        foreach ($session_contacts as $k => $a) {
                            if ($a['id'] == $co) {
                                $_SESSION['session_contacts'][$k]['name'] = $name_list[$key];
                                $_SESSION['session_contacts'][$k]['email'] = $email_list[$key];
                                $_SESSION['session_contacts'][$k]['fone'] = $fone_list[$key];
                            }
                        }
                    }
                    $_SESSION["message"] = array('type' => 'ok', 'message' => "Alterações realizadas com sucesso!");
                    header("location: import.php");
                } else if ($action == 'excluir') {
                    $id_list = $_POST['id_contact'];
                    $session_contacts = $_SESSION['session_contacts'];
                    foreach ($id_list as $key => $co) {
                        foreach ($session_contacts as $k => $a) {
                            if ($a['id'] == $co) {
                                unset($_SESSION['session_contacts'][$k]);
                            }
                        }
                    }
                    if(count($_SESSION["session_contacts"])==0) {
                        unset($_SESSION["session_contacts"]);
                    }
                    $_SESSION["message"] = array('type' => 'ok', 'message' => "Operação realizadas com sucesso!");
                    header("location: import.php");
                } else if ($action == 'salvar') {

                    $id_list = $_POST['id_contact'];
                    $session_contacts = $_SESSION['session_contacts'];
                    foreach ($id_list as $key => $co) {
                        foreach ($session_contacts as $k => $a) {
                            if ($a['id'] == $co) {
                                $con = new Contact();
                                $con->setName($a['name']);
                                $con->setEmail($a['email']);
                                $con->setFone($a['fone']);
                                $con->setUser($_SESSION['usuario']->getId());
                                echo $con->insert();
                                unset($_SESSION['session_contacts'][$k]);
                            }
                        }
                    }
                    if(count($_SESSION["session_contacts"])==0) {
                        unset($_SESSION["session_contacts"]);
                    }
                    $_SESSION["message"] = array('type' => 'ok', 'message' => "Contatos importados para sua agenda com sucesso!");
                    header("location: import.php");
                }
            }
        }
        ?>
        <?php ?>
        <?php
    }
    require 'rodape.php';
    ?>
<script src="js/jquery.maskedinput.js"></script> 